// pages/myCollection/myCollection.js
var util = require('../../utils/request.js');
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    edirOrfinish: '编辑',
    show: false,
    collection: [],
    pageNumber: 1,
    pageSize: 5,
    hasMoreData: false,
    selectedList: [],
    items: [],
    startX: 0, //开始坐标
    startY: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getCollection();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (that.data.hasMoreData) {
      wx.showLoading({
        title: '正在加载...',
      })
      this.getCollection();
    } else {
      wx.showToast({
        title: '没有更多数据了!',
        icon: 'none',
        duration: 800
      })
    }
  },
  // 导航
  navigation(e) {
    console.log(e);
    var currentIndex = e.currentTarget.dataset.index;
    var collection = this.data.collection;
    if (this.data.edirOrfinish == '编辑') {
      // console.log(e.currentTarget.dataset.address)
      wx.openLocation({
        latitude: collection[currentIndex].lat,
        longitude: collection[currentIndex].lng,
        name: collection[currentIndex].name,
        address: collection[currentIndex].address
      })
    } else {
      this.select(e);
    }
  },
  // 编辑
  edit: function() {
    if (this.data.edirOrfinish == '编辑') {
      this.setData({
        edirOrfinish: '完成',
        show: true
      })
    } else {
      this.setData({
        edirOrfinish: '编辑',
        show: false
      })
    }
  },
  // 选择收藏
  select: function(e) {
    console.log(e);
    var collectid = e.currentTarget.dataset.collectid
    var image = e.currentTarget.dataset.imgsrc
    var img = "collection[" + e.currentTarget.id + "].selectImg";
    var selectedList = this.data.selectedList;
    var that = this
    if (image == '../../img/wxz@3x.png') {
      selectedList.push(collectid);
      console.log(selectedList);
      this.setData({
        [img]: '../../img/xz@3x.png',
        selectedList: selectedList
      })
    } else {
      this.setData({
        [img]: '../../img/wxz@3x.png'
      })
      this.removeByValue(selectedList, collectid);
      console.log(selectedList);
    }
  },
  // 删除数组中指定的值(固定方法)
  removeByValue(arr, val) {
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] == val) {
        arr.splice(i, 1);
        break;
      }
    }
  },
  // 删除
  deleteCollection(e) {
    var that = this;
    var selectedList = this.data.selectedList;
    var collection = that.data.collection.reverse();
    console.log(collection);
    console.log(selectedList);
    if (selectedList.length == 0) {
      wx.showToast({
        title: '至少选择一个哦！',
        icon: 'none'
      })
    } else {
      wx.showLoading({
        title: '正在删除...',
      })
      util.req('/collect/delCollect', {
        'collectIds': selectedList
      }, function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.resultCode == 200) {
          wx.showToast({
            title: res.resultMsg,
          })
          that.setData({
            collection: [],
            pageNumber: 1,
            pageSize: 5,
            hasMoreData: false,
          })
          // setTimeout(function() {
          that.getCollection()
          // }, 1000)
        } else {
          wx.showToast({
            title: res.resultMsg,
            icon: 'none'
          })
        }
      })
    }

  },
  // 获取收藏列表
  getCollection() {
    var that = this;
    var pageNumber = this.data.pageNumber;
    var pageSize = this.data.pageSize;
    var collection = this.data.collection;
    var data = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'memberId': globalData.memberId
    }
    console.log(data);
    util.req('/collect/myCollect', data, function(res) {
      console.log(res);
      if (res.resultCode == 200) {
        res.resultContent.map(function(item) {
          item.selectImg = '../../img/wxz@3x.png';
          item.isTouchMove = false;
        })
        if (res.resultContent.length < pageSize) {
          that.setData({
            collection: collection.concat(res.resultContent),
            hasMoreData: false
          })
        } else {
          that.setData({
            collection: collection.concat(res.resultContent),
            hasMoreData: true,
            pageNumber: pageNumber + 1
          })
        }
      } else {
        wx.showToast({
          title: res.resultMsg,
        })
      }
      wx.hideLoading();
    })
  },
  //手指触摸动作开始 记录起点X坐标
  touchstart: function(e) {
    //开始触摸时 重置所有删除
    this.data.collection.forEach(function(v, i) {
      if (v.isTouchMove) //只操作为true的
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      collection: this.data.collection
    })
  },
  //滑动事件处理
  touchmove: function(e) {
    var that = this,
      index = e.currentTarget.dataset.index, //当前索引
      startX = that.data.startX, //开始X坐标
      startY = that.data.startY, //开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    that.data.collection.forEach(function(v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      collection: that.data.collection
    })
  },
  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function(start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  //删除事件
  del: function(e) {
    var that = this;
    var currentIndex = e.currentTarget.dataset.index;
    var collection = that.data.collection.reverse();
    var selectedList = this.data.selectedList;
    var url = '/collect/delCollect';
    var paramsData = {
      collectIds: collection[currentIndex].collectId
    };
    wx.showLoading({
      title: '正在删除...',
    })
    util.req(url, paramsData, function(res) {
      console.log(res);
      wx.hideLoading();
      if (res.resultCode == 200) {
        wx.showToast({
          title: res.resultMsg,
        })
        that.setData({
          collection: [],
          pageNumber: 1,
          pageSize: 5,
          hasMoreData: false,
        })
        // setTimeout(function() {
        that.getCollection()
        // }, 1000)
      } else {
        wx.showToast({
          title: res.resultMsg,
          icon: 'none'
        })
      }
    })
  }
})